package com.yunkeji.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.exception.ServiceException;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.broker.BrokerMenu;
import com.yunkeji.dao.domain.broker.BrokerUser;
import com.yunkeji.dao.domain.broker.BrokerUserMenu;
import com.yunkeji.dao.mapper.broker.BrokerMenuMapper;
import com.yunkeji.dao.mapper.broker.BrokerUserMapper;
import com.yunkeji.dao.mapper.broker.BrokerUserMenuMapper;
import com.yunkeji.system.service.IBrokerUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 推广用户 业务层处理
 *
 * @author 雷擎宇
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BrokerUserServiceImpl implements IBrokerUserService {

    private final BrokerUserMapper baseMapper;
    private final BrokerMenuMapper menuMapper;
    private final BrokerUserMenuMapper userMenuMapper;

    /**
     * 新增用户
     */
    @Override
    public int insertUser(BrokerUser user) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<BrokerUser>()
            .eq(BrokerUser::getUserName, user.getUserName())
            .ne(ObjectUtil.isNotNull(user.getUserId()), BrokerUser::getUserId, user.getUserId()));
        if(exist){
            throw new ServiceException("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        int rows = baseMapper.insert(user);
        //查询所有推广菜单
        List<BrokerMenu> menus = menuMapper.selectList();
        //初始化该用户的菜单
        List<BrokerUserMenu> userMenus = new ArrayList<>(menus.size());
        menus.forEach(menu -> {
            BrokerUserMenu userMenu = new BrokerUserMenu();
            userMenu.setMenuId(menu.getMenuId());
            userMenu.setUserId(user.getUserId());
            userMenus.add(userMenu);
        });
        userMenuMapper.insertBatch(userMenus);
        return rows;
    }

    /**
     * 修改用户
     */
    @Override
    public int updateUser(BrokerUser user) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<BrokerUser>()
            .eq(ObjectUtil.isNotNull(user.getUserName()), BrokerUser::getUserName, user.getUserName())
            .ne(ObjectUtil.isNotNull(user.getUserId()), BrokerUser::getUserId, user.getUserId()));
        if(exist){
            throw new ServiceException("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        return baseMapper.updateById(user);
    }

    /**
     * 查询用户列表
     * @param user
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<BrokerUser> selectPageUserList(BrokerUser user, PageQuery pageQuery) {
        Page<BrokerUser> page = baseMapper.selectVoPage(
            pageQuery.build(),Wrappers.lambdaQuery(BrokerUser.class)
                .eq(BrokerUser::getDelFlag, 0)
                .like(StringUtils.isNotEmpty(user.getUserName()), BrokerUser::getUserName, user.getUserName())
                .eq(ObjectUtil.isNotNull(user.getBranch()), BrokerUser::getBranch, user.getBranch())
                .orderByDesc(BrokerUser::getCreateTime));
        return TableDataInfo.build(page);
    }

    /**
     * 通过用户ID删除用户
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteUserById(Long userId) {
        return baseMapper.deleteById(userId);
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public BrokerUser selectUserById(Long userId) {
        return baseMapper.selectById(userId);
    }
}
